package com.example.beson.androiddemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.beson.androiddemo.fragment.AndroidFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 仿京东双十一等节日切换图标
 */
public class TabButtonActivity extends AppCompatActivity {

    @BindView(R.id.home_main)
    FrameLayout homeMain;
    @BindView(R.id.button_home_image)
    ImageView buttonHomeImage;
    @BindView(R.id.button_home)
    RelativeLayout buttonHome;
    @BindView(R.id.button_school_image)
    ImageView buttonSchoolImage;
    @BindView(R.id.button_school)
    RelativeLayout buttonSchool;
    @BindView(R.id.button_message_image)
    ImageView buttonMessageImage;
    @BindView(R.id.button_message)
    RelativeLayout buttonMessage;
    @BindView(R.id.button_mine_image)
    ImageView buttonMineImage;
    @BindView(R.id.button_mine)
    RelativeLayout buttonMine;
    public int currentFragmentType = -1;

    private AndroidFragment androidFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_button);
        ButterKnife.bind(this);

        FragmentManager fragmentmanager = getSupportFragmentManager();
        currentFragmentType = 1;
        if (savedInstanceState != null) {
            int type = savedInstanceState.getInt("currentFragmentType");
            androidFragment = (AndroidFragment) fragmentmanager.findFragmentByTag("home");
            if (type > 0) {
                selectHome();
            }
        } else {
            FragmentTransaction transaction = fragmentmanager.beginTransaction();
            Fragment publishfrag = fragmentmanager.findFragmentByTag("home");
            if (publishfrag != null) {
                transaction.replace(R.id.home_main, publishfrag);
                transaction.commit();
            } else {
                selectHome();
            }
        }
    }

    @OnClick({R.id.button_home, R.id.button_school, R.id.button_message, R.id.button_mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button_home:
                selectHome();
                break;
            case R.id.button_school:
                updateHome(false);
                updateSchool(true);
                updateMessage(false);
                updateMine(false);
                loadFragment(2);
                break;
            case R.id.button_message:
                updateHome(false);
                updateSchool(false);
                updateMessage(true);
                updateMine(false);
                loadFragment(3);
                break;
            case R.id.button_mine:
                updateHome(false);
                updateSchool(false);
                updateMessage(false);
                updateMine(true);
                loadFragment(4);
                break;
        }
    }
    private void selectHome(){
        updateHome(true);
        updateSchool(false);
        updateMessage(false);
        updateMine(false);
        loadFragment(1);
    }
    private void updateHome(boolean isSelect) {
        long nowTime=System.currentTimeMillis()/1000l;
        if(1493025300<nowTime&&nowTime<1493025900){
            if (isSelect) {
                buttonHomeImage.setImageDrawable(getResources().getDrawable(R.mipmap.tab_shouye_s));
            } else {
                buttonHomeImage.setImageDrawable(getResources().getDrawable(R.mipmap.tab_shouye));
            }
        }else{
            if (isSelect) {
                buttonHomeImage.setImageDrawable(getResources().getDrawable(R.mipmap.hp_hp_y));
            } else {
                buttonHomeImage.setImageDrawable(getResources().getDrawable(R.mipmap.hp_hp_n));
            }
        }

    }
    private void updateSchool(boolean isSelect) {
        long nowTime=System.currentTimeMillis()/1000l;
        if(1493025300<nowTime&&nowTime<1493025900){
            if (isSelect) {
                buttonSchoolImage.setImageDrawable(getResources().getDrawable(R.mipmap.tab_baohe_s));
            } else {
                buttonSchoolImage.setImageDrawable(getResources().getDrawable(R.mipmap.tab_baohe));
            }
        }else{
            if (isSelect) {
                buttonSchoolImage.setImageDrawable(getResources().getDrawable(R.mipmap.hp_school_y));
            } else {
                buttonSchoolImage.setImageDrawable(getResources().getDrawable(R.mipmap.hp_school_n));
            }
        }

    }
    private void updateMessage(boolean isSelect) {
        long nowTime=System.currentTimeMillis()/1000l;
        if(1493025300<nowTime&&nowTime<1493025900){
            if (isSelect) {
                buttonMessageImage.setImageDrawable(getResources().getDrawable(R.mipmap.tab_shequ_s));
            } else {
                buttonMessageImage.setImageDrawable(getResources().getDrawable(R.mipmap.tab_shequ));
            }
        }else{
            if (isSelect) {
                buttonMessageImage.setImageDrawable(getResources().getDrawable(R.mipmap.hp_news_y));
            } else {
                buttonMessageImage.setImageDrawable(getResources().getDrawable(R.mipmap.hp_news_n));
            }
        }

    }
    private void updateMine(boolean isSelect) {
        long nowTime=System.currentTimeMillis()/1000l;
        if(1493025300<nowTime&&nowTime<1493025900){
            if (isSelect) {
                buttonMineImage.setImageDrawable(getResources().getDrawable(R.mipmap.tab_wode_s));
            } else {
                buttonMineImage.setImageDrawable(getResources().getDrawable(R.mipmap.tab_wode));
            }
        }else{
            if (isSelect) {
                buttonMineImage.setImageDrawable(getResources().getDrawable(R.mipmap.hp_me_y));
            } else {
                buttonMineImage.setImageDrawable(getResources().getDrawable(R.mipmap.hp_me_n));
            }
        }

    }

    private void loadFragment(int type){
        FragmentManager fragmentmanager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentmanager.beginTransaction();
        if(type==1){
            if(androidFragment==null){
                androidFragment=new AndroidFragment();
                transaction.add(R.id.home_main,androidFragment,"home");
            }else{
                transaction.show(androidFragment);
            }
            currentFragmentType=1;
        }
        if(type==2){
            if(androidFragment==null){
                androidFragment=new AndroidFragment();
                transaction.add(R.id.home_main,androidFragment,"school");
            }else{
                transaction.show(androidFragment);
            }

            currentFragmentType=2;
        }
        if(type==3){
            if(androidFragment==null){
                androidFragment=new AndroidFragment();
                transaction.add(R.id.home_main,androidFragment,"message");
            }else{
                transaction.show(androidFragment);
            }
            currentFragmentType=3;
        }
        if(type==4){
            if(androidFragment==null){
                androidFragment=new AndroidFragment();
                transaction.add(R.id.home_main,androidFragment,"mine");
            }else{
                transaction.show(androidFragment);
            }
            currentFragmentType=4;
        }
        transaction.commitAllowingStateLoss();
    }
}
